
import UIKit

extension UIView{
    
    /// View 快照/截图
    func bl_snapshotImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0)
        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let snap = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snap
    }
    
    /// 设置LayerShadow,offset,radius
    func bl_setLayerShadow(color : UIColor, offset : CGSize,radius : CGFloat) {
        self.layer.shadowColor = color.CGColor
        self.layer.shadowOffset = offset
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = 1
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
    
    /// 移除View全部子控件
    func bl_removeAllSubviews() {
        for _ in 0..<self.subviews.count {
            subviews.last?.removeFromSuperview()
        }
    }    
    
    /// 返回目标View所在的控制器UIViewController
    func bl_viewController() -> UIViewController? {
        if self.superview == nil {
            return (self.nextResponder() as! UIViewController)
        }
        for var next = self.superview; (next != nil); next = next!.superview {
            let nextResponder: UIResponder = next!.nextResponder()!
            if (nextResponder as? UIViewController != nil) {
                return (nextResponder as! UIViewController)
            }
        }
        return nil
    }
}

